<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
'use strict';

function run_test(test) {
  var did_load_event_fire = false;
  var got_message_from_child = false;

  function check_if_test_is_done() {
    if (did_load_event_fire && got_message_from_child) {
      test.done();
    }
  }

  // Add a cross-site subframe that will post back a message
  // from an inline script.
  const frame = document.createElement('iframe');
  frame.src = (location.protocol + '//localhost:' + location.port +
              '/misc/resources/' +
              'subframe-for-load-event-after-removing-child-frame.html');
  document.body.appendChild(frame);

  // The message from the subframe should hopefully reach back here
  // while the subframe is still loading (this is race-y and can only
  // happen with OOPIFs).
  window.addEventListener('message', test.step_func(evt => {
      assert_equals(evt.data, 'childmessage');

      // Remove the subframe while it is (hopefully - see above) still loading.
      //
      // If the loading state of the removed subframe was the only thing
      // preventing *our* 'load' event from being fired, then our 'load'
      // event should be fired now.  See also https://crbug.com/779433.
      document.body.removeChild(frame);

      got_message_from_child = true;
      check_if_test_is_done();
  }));

  // Verify that the 'load' event gets fired.
  window.addEventListener('load', test.step_func(evt => {
      did_load_event_fire = true;
      check_if_test_is_done();
  }));
}

async_test(run_test, 'Got load event.');

</script>
</body>
